<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>商品资料编辑-WeAdmin Frame型后台管理系统-WeAdmin 1.0</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../../static/css/font.css">
  <link rel="stylesheet" href="../../../static/css/weadmin.css">
</head>

<body>
  <div class="weadmin-body">
    <form class="layui-form">
      <div class="layui-form-item">
        <label for="L_username" class="layui-form-label">
          <span class="we-red">*</span>商品名称
        </label>
        <div class="layui-input-inline">
          <input type="text" id="L_username" lay-verify="required" name="name" autocomplete="off" class="layui-input">
        </div>
      </div>

      <div class="layui-form-item">
        <label class="layui-form-label">
          <span class="we-red">*</span>商品类型
        </label>
        <div class="layui-input-inline">
          <input type="text" id="L_phone" lay-verify="required" name="category" autocomplete="off" class="layui-input">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">
          <span class="we-red">*</span>商品价格
        </label>
        <div class="layui-input-inline">
          <input type="text" id="L_price" lay-verify="required|price" name="price" autocomplete="off" class="layui-input">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">
        </label>
        <button class="layui-btn" lay-filter="add" lay-submit="">确定</button>
        <input type="hidden" name="dataId" id="dataId" value="" />
      </div>
    </form>
  </div>
  <script src="../../../static/lib/layui/layui.js" charset="utf-8"></script>
  <script type="text/javascript">
    layui.extend({
      admin: '{/}../../static/js/admin'
    });
    layui.use(['form', 'jquery', 'admin', 'layer'], function () {
      var form = layui.form,
        $ = layui.jquery,
        admin = layui.admin,
        layer = layui.layer;

      //自定义验证规则
      form.verify({
        price: [/^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/, '价格只能有数字，且最多两位小数']
      });

      //页面初始化加载
      $(function () {
        setTimeout(function () {
          frameVal();
        }, 100);
      });
      function frameVal() {
        var dataId = $('input[name="dataId"]').val();
        var index = parent.layer.getFrameIndex(window.name);
        parent.layui.jquery("#productList tr").each(function () {
          if ($(this).attr('data-id') == dataId) {
            var tdArr = $(this).children('td');
            var name = tdArr.eq(2).text(); //名字
            var category = tdArr.eq(3).text(); //类别
            var price = tdArr.eq(4).text(); //价格
            $('input[name="name"]').val(name);
            $('input[name="category"]').val(category);
            $('input[name="price"]').val(price);
            form.render();
          }
        });
      }
      //监听提交
      form.on('submit(add)', function (data) {
        var dataId = $('input[name="dataId"]').val();
        var f = data.field;
        var product_data = {productName: f.name,category: f.category,price: f.price}
        $.post({
          url: "/product/update/"+dataId,
          contentType: "application/json;charset=UTF-8",
          data: JSON.stringify(product_data),
          success: function () {
            parent.layui.jquery("#productList tr").each(function () {
              if ($(this).attr('data-id') == dataId) {
                var tdArr = $(this).children('td');
                tdArr.eq(2).text(f.name);
                tdArr.eq(3).text(f.category);
                tdArr.eq(4).text(f.price);
                form.render();
              }
            });
          }
        })
        layer.alert("修改成功", {
          icon: 6
        }, function () {
          var index = parent.layer.getFrameIndex(window.name);
          parent.layer.close(index);
        });
        return false;
      });

    });
  </script>
</body>

</html>